﻿package app.demo.MyTouchApp{
 
    import flash.events.TUIO; // allows to connect to touchlib/tbeta
 
    import flash.display.*;
    import flash.events.*;
	import flash.events.MouseEvent;
    import flash.geom.*; // needed for curPt;
	import flash.display.SimpleButton;
	import flash.net.URLRequest;
	import flash.display.FrameLabel;
	import fl.controls.Label;
	import fl.controls.Button;
	import fl.controls.*;
	import fl.core.*;
	import flash.display.Sprite;
	import flash.text.*;
	import flash.system.fscommand;
	import flash.system.*;
	import fl.managers.StyleManager;
	//import flash.display.Stage.displayState;
	import flash.display.Stage;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	
 
    public class MyTouchApp extends MovieClip {
 
 		//private static var STAGE:Stage;
 		//StageDisplayState.FULL_SCREEN;
			
        private var color:uint = 0xff0000; // color of lines
        private var thickness:Number = 30; // thickness of lines
        private var blobs:Array = new Array(); // blobs we are currently interacting with
        private var lines:Array = new Array(); // array to hold the lines
		private var numButtonWidth:Number = 100;
		private var numButtonHeight:Number = 50;
		private var images:Array = ["shapes.png", "darkBackground.png", "matching1.png", "darkBackground.png", "title.png"];
		var image1:Loader = new Loader();
		var image3:Loader = new Loader();
		var Label1:Label = new Label();
		var movie1:MovieClip = new MovieClip();
		var HOL:Loader = new Loader();
		var buttonExit:Button;
		
		var myMaster:master;
		
		var flag:Boolean=true;
		
        public function MyTouchApp(m:master):void {
			
			
			
			//fscommand("allowscale", "false");
			//fscommand("fullscreen", "false");
			//Stage.showMenu = false;
			
			//Stage.displayState = "fullScreen";
			
			
 			myMaster=m;
            //--------connect to TUIO-----------------
            TUIO.init(this,'localhost',3000,'',true);
            trace("MyTouchApp Initialized");
            //---------------------------------------- 
			
			//this.displayState = "fullScreen"
 			
			
			var tf:TextFormat = new TextFormat();
			tf.bold = true;
			tf.font = "Kristen ITC";
			tf.size = 14;
			//tf.color= 0xffffff; //white
			tf.color=0x000000; //black
			StyleManager.setStyle("textFormat", tf);
			
			trace("trace bla");
			trace(this.width);
			
			//Adding the first movie
			addChild(movie1);
			
            //addEventListener(TouchEvent.MOUSE_DOWN, downEvent, false, 0, true); // run when finger down
            addEventListener(TouchEvent.MOUSE_UP, upEvent, false, 0, true); // run when finger up
            addEventListener(Event.ENTER_FRAME, updater, false, 0, true); // keep running
			addEventListener(Event.ADDED, addedHandler);
			
			//Image 1
			image1.load(new URLRequest(images[0]));
			//image1.x=70;
			//image1.y=200;
			image1.scaleX=0.7;
			image1.scaleY=0.7;
			image1.addEventListener(TouchEvent.MOUSE_DOWN,goToNext1);
			//image1.height=300;
			
			
			// image 2
			var image2:Loader = new Loader();
			image2.load(new URLRequest(images[1]));
			movie1.addChild(image2);
			
			
			//movie1.addChild(image1);
			///////////////////////////
			//Image 3
			
			image3.load(new URLRequest(images[2]));
			image3.x=520;
			image3.y=200;
			image3.scaleX=0.7;
			image3.scaleY=0.7;
			//image1.height=300;
			image3.addEventListener(TouchEvent.MOUSE_DOWN,goToNext);
			movie1.addChild(image3);
			/////////////////////
			
			//Title of the Page"
			var myTextFormat:TextFormat = new TextFormat();
			myTextFormat.font = "Kristen ITC";
			myTextFormat.size = 50;
			Label1.text = "Hands On Learning";
			Label1.width=700;
			Label1.height=300;
			Label1.x=260;
			Label1.visible=true;
			Label1.setStyle("textFormat", myTextFormat);
			//movie1.addChild(Label1);
			
			HOL.load(new URLRequest(images[4]));
			HOL.x=240;
			movie1.addChild(HOL);
			
			
			
			var myTextFormat2:TextFormat = new TextFormat();
			myTextFormat2.font = "Kristen ITC";
			myTextFormat2.size = 30;
			
			//var styleObject:Object = Button.getStyleDefinition();
			//var defaultEmphasizedPadding:uint = styleObject.emphasizedPadding;
			
			//Exit button:
			buttonExit = new Button;
			buttonExit.x=900;
			buttonExit.y=650;
			buttonExit.width=100;
			buttonExit.height=40;
			buttonExit.label="EXIT";
			//buttonExit.emphasized=true;
			//buttonExit.setStyle("emphasizedPadding", defaultEmphasizedPadding);
			//buttonExit.setStyle= ("textFormat", myTextFormat2);
			buttonExit.addEventListener(TouchEvent.MOUSE_DOWN,closeApp);
			movie1.addChild(buttonExit);
			
			
	}
	   private function addedHandler(event:Event):void {
        trace("addedHandler: " + event);
        //stage.scaleMode = StageScaleMode.NO_SCALE;
        //stage.align = StageAlign.TOP_LEFT;
       
    }
	
		
		/////////////////////////
		private function resizeHandler(event:Event):void {
            trace("resizeHandler: " + event);
            trace("stageWidth: " + myMaster.stage.width + " stageHeight: " + myMaster.stage.height);
        }
		/////////////////////////
		function closeApp(event:TouchEvent):void
		{
     		 fscommand("quit");    
     	}
		
		private function goToNext(e:TouchEvent):void
		{
			trace("blablbabla");
			trace(stage.width);
			myMaster.goToMain();
		}
		private function goToNext1(e:TouchEvent):void
		{
			myMaster.goToShapesMain();
		}
		
		private function drawButtonState(rgb:uint):Sprite {
		var sprite:Sprite = new Sprite();
		//sprite.graphics.lineStyle(4,0x33621E,1);
		sprite.graphics.beginFill(rgb);
		sprite.graphics.drawRoundRect(((this.stage.
		stageWidth/2)-(numButtonWidth/2)),((this.
		stage.stageHeight/2)-(numButtonHeight/2)),
		numButtonWidth,numButtonHeight,10,10);
		return sprite;
		}
 
        // updates/draws the drawn lines by:
        // cycling through all existing blobs
        // getting the id of each blob
        // use that id to get the line, as the lines are named after their blobs (eg line1280 )
        // tuioobj is the blob - see how it is called by request using the blob id?
        // if it is not there remove it from the blobs array.
        // draw the next segment of the line using lineTo and current loc (tuioobj.x, tuioobj.y)
        function updater(e:Event):void {
            for (var i:int = 0; i < blobs.length; i++) { 
                var tuioobj:TUIOObject=TUIO.getObjectById(blobs[i].id);
 
                // if not found, then it must have died..
                if (! tuioobj) {
                    removeBlob(blobs[i].id);
                } else if (parent != null) {
 
                    lines["line"+blobs[i].id].graphics.lineTo(tuioobj.x, tuioobj.y);
                }
            }
			
			if(flag==true){
			trace("enter frame");
			trace(stage.width);
			stage.addEventListener("resize", resizeHandler);
			resizeObject(stage.width,stage.height);
			}
			
			flag=false;
        }
		
		function resizeObject(w:int, h:int)
		{
			image1.x=0.068*w;
			image1.y=0.26*h;
			movie1.addChild(image1);
		}
		/*function start():void{
			var aButton:SimpleButton=new SimpleButton;
			

			aButton.addEventListener(MouseEvent.CLICK, clickHandler);
			//aButton.label = "Submit";
			addChild(aButton);
		}*/
 
        // when touched, this calls addblob with the blob ID and the X/Y of where it started
        public function downEvent(e:TouchEvent):void {
            var curPt:Point=parent.globalToLocal(new Point(e.stageX,e.stageY));
            addBlob(e.ID, curPt.x, curPt.y);
            e.stopPropagation();
        }
		
		function clickHandler(evtObj:MouseEvent){
		trace("The " + evtObj.target.label + " button was clicked");
		}
 
        // When a finger stops touching, call removeBlob
        public function upEvent(e:TouchEvent):void {
            removeBlob(e.ID);
            e.stopPropagation();
        }
 
        // This takes the sent ID, x,y and puts it all in the blobs array
        // It then creats a new sprite, and sets the starting loc of the line
        // The line is put in an array called lines, so it can be called over and over by name
        // if the lines did not have unique names you could only draw one line at a time
        // so each line is named in the array after the id of the blob (EG line1280)
        function addBlob(id:Number, origX:Number, origY:Number):void {
            blobs.push( {id: id, origX: origX, origY: origY, myOrigX: x, myOrigY:y} );
 
            var line:Sprite = new Sprite();
            lines["line"+id] = line;
            lines["line"+id].graphics.lineStyle(thickness, color); //<-- config above
            lines["line"+id].graphics.moveTo(origX, origY); //starting point of line to be drawn
            addChild(lines["line"+id]); // add line to stage
        }
 
        // when called, this function removes the blobs from the array
        // cycles through the blobs array until the blob is found by its ID, then it is removed.
        function removeBlob(id:Number):void {
            for (var i=0; i < blobs.length; i++) {
                if (blobs[i].id==id) {
                    blobs.splice(i, 1);
                    return;
                }
            }
        }
 
    }
}